/*
 * copyright (c) 2009 Alexandr [Sn@ble] Surnin
 *
 * This file is part of FrameSeeker.
 *
 * FrameSeeker is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FrameSeeker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You can read the full version of GNU LGPL in
 * http://www.gnu.org/copyleft/lesser.html
 */


#include "FSXMLWriter.h"

FSXMLWriter::FSXMLWriter(const QString &_fileName) {

    setFileName(_fileName);
    setIndentSize(4);
    nestingLevel = 0;
}


FSXMLWriter::~FSXMLWriter() {

    out.flush();
    outFile.flush();
    outFile.close();
}


QString FSXMLWriter::indent() const {

    return QString(nestingLevel * indentSize, ' ');
}


void FSXMLWriter::setIndentSize(int indSize) {

    indentSize = indSize;
}


void FSXMLWriter::writeLine(const QString &str) {

    if (str.startsWith("</")) {
        nestingLevel--;
    }

    out << indent() << str << "\n";

    if (str.startsWith("<?")) {
        return;
    } else if (!str.contains("</")) {
        nestingLevel++;
    }
}

void FSXMLWriter::writeLine(const char* tag, const int value) {

    writeLine(tag, QString(value));
}

void FSXMLWriter::writeLine(const char* tag, const QString &value) {

    out << indent()
        << "<"  << tag << ">"
        <<        value
        << "</" << tag << ">"
        << "\n";
}


void FSXMLWriter::writeHeader() {

    writeLine("<?xml version=\"1.0\" encoding=\"UTF-8\">");
    writeLine("<frame>");
}


void FSXMLWriter::writeFooter() {

    writeLine("</frame>");
}


void FSXMLWriter::writeMotionVector(FSMotionVector &mv) {

    writeLine("<MotionVector>");
        writeLine("<MB>");
            writeLine("x", mv.getMB().x());
            writeLine("y", mv.getMB().y());
       writeLine("</MB>");
       writeLine("<MV>");
            writeLine("x", mv.getMV().x());
            writeLine("y", mv.getMV().y());
       writeLine("</MV>");
    writeLine("</MotionVector>");
}


void FSXMLWriter::setFileName(const QString &_fileName) {

    fileName = _fileName + ".xml";
}



